<template>
  <el-card class="tableCard">
    <h2 style="margin-bottom:20px; text-align:center">
      <svg t="1643038653606" class="icon" viewBox="0 0 38912 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6866" width="200" height="200"><path d="M1228.8 409.6h37683.2v204.8H1228.8V409.6zM512 0A512 512 0 1 1 0 512 512 512 0 0 1 512 0z" p-id="6867" fill="#283618"></path></svg>
      管理员列表
      <svg t="1643038691074" class="icon" viewBox="0 0 38707 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7094" width="200" height="200"><path d="M0 409.6h37683.2v204.8H0V409.6z m38195.2-409.6a512 512 0 1 1-512 512 512 512 0 0 1 512-512z" p-id="7095" fill="#283618"></path></svg>
    </h2>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="open = true"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        class="right-toolbar"
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="userList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column
        label="用户编号"
        align="center"
        key="userid"
        prop="userid"
        v-if="columns[0].visible"
      />
      <el-table-column
        label="用户名"
        align="center"
        key="loginname"
        prop="loginname"
        v-if="columns[1].visible"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="真实姓名"
        align="center"
        key="realname"
        prop="realname"
        v-if="columns[2].visible"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="性别"
        align="center"
        key="sex"
        prop="sex"
        v-if="columns[4].visible"
        width="120"
        :formatter="customerSexFormat"
      />
      <el-table-column
        label="手机号码"
        align="center"
        key="mobilephone"
        prop="mobilephone"
        v-if="columns[4].visible"
        width="120"
      />
      <el-table-column
        label="角色"
        align="center"
        key="groupid"
        prop="groupid"
        v-if="columns[4].visible"
        width="120"
        :formatter="roleFormat"
      />
      <el-table-column
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            v-if="scope.row.userId !== 1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-key"
            @click="handleResetPwd(scope.row)"
            >重置</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      class="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="修改用户信息"
      :visible.sync="open"
      width="600px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="loginname">
          <el-input v-model="form.loginname" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realname">
          <el-input v-model="form.realname" maxlength="11" />
        </el-form-item>
        <el-form-item label="手机号码" prop="mobilephone">
          <el-input v-model="form.mobilephone" maxlength="50" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="form.sex">
            <el-radio label="0">女</el-radio>
            <el-radio label="1">男</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="form.groupid" placeholder="权限角色">
            <el-option
              v-for="item in roleOptions"
              :key="item.groupid"
              :label="item.groupname"
              :value="item.groupid"
              :title="item.description"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="submit">保存</el-button>
          <el-button type="danger" size="mini" @click="close">关闭</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  listUser,
  updateProfile,
  resetUserPwd,
  addUser,
  removeUser,
} from "@/api/user";
import { getRoleGroup } from "@/api/rolegroup";
export default {
  data() {
    return {
      open: false,
      total: 1,
      loading: false,
      showSearch: true,
      multiple: false,
      // 选中数组
      ids: [],
      // 是否为新增
      isAdd: false,
      columns: [
        { key: 0, label: `用户编号`, visible: true },
        { key: 1, label: `用户名称`, visible: true },
        { key: 2, label: `用户昵称`, visible: true },
        { key: 3, label: `部门`, visible: true },
        { key: 4, label: `手机号码`, visible: true },
        { key: 5, label: `状态`, visible: true },
        { key: 6, label: `创建时间`, visible: true },
      ],
      userList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        loginname: undefined,
        realname: undefined,
        mobilephone: undefined,
        groupid: undefined,
        sex: undefined,
      },
      form: {},
      // 角色列表
      roleOptions: [],
      rules: {
        loginname: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        realname: [
          { required: true, message: "真实姓名不能为空", trigger: "blur" }
        ],
        mobilephone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    getList() {
      this.loading = true;
      listUser(this.queryParams).then((res) => {
        this.responseInterceptor(res);
        this.total = res.data.total;
        this.userList = res.data.rows;
        this.loading = false;
      });
    },
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        loginname: undefined,
        realname: undefined,
        mobilephone: undefined,
        groupid: undefined,
        sex: undefined,
      };
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.multiple = selection.length > 1;
      this.form = selection[0] || {};
    },
    handleAdd() {
      this.form = { pwd: "123456", status: "1" };
      this.open = true;
      this.isAdd = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.form = row;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        removeUser(row.userid).then((res) => {
          this.responseInterceptor(res);
          if (res.data.code == 200) {
            this.$message.success(res.data.msg);
          }
        }).then(()=>{
            this.getList();
        });
      });
    },
    handleResetPwd(row) {
      if (row.userid == this.$store.state.permission.userInfo.userid) {
        this.$confirm(
          "您正在重置当前用户的密码, 重置后将强制退出, 是否继续?",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(() => {
            this.resetPwd(row.userid);
            setTimeout(() => {
              this.$router.push("/login");
            }, 1000);
          })
          .catch(() => {
            console.log(666);
            return;
          });
      } else {
        this.resetPwd(row.userid);
      }
    },
    resetPwd(userid) {
      resetUserPwd(userid).then((res) => {
        this.responseInterceptor(res);
        if (res.data.code == 200) {
          this.$message.success(res.data.msg);
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$message.warning("敬请期待");
    },
    customerSexFormat(row) {
      return row.sex === "1" ? "男" : "女";
    },
    roleFormat(row) {
      console.log(row)
      return this.roleOptions.length > 0
        ? this.roleOptions.filter((item) => item.groupid == row.groupid)[0].groupname
        : "";
    },
    submit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.isAdd) {
            this.form.userid = ''
            addUser(this.form).then((res) => {
              if (res.data.code == 200) {
                this.$message.success(res.data.msg);
                this.getList();
                this.close();
              }
            });
            return;
          }
          // 修改信息
          updateProfile(this.form).then((res) => {
            this.responseInterceptor(res);
            if (res.data.code == 200) {
              this.$message.success(res.data.msg);
              this.close();
            }
          });
        }
      });
    },
    close() {
      this.form = {};
      this.open = false;
    },
  },
  mounted() {
    this.getList();
    getRoleGroup().then((res) => {
      this.responseInterceptor(res);

      this.roleOptions = res.data.rows;
    });
  },
};
</script>

<style lang="scss" scoped>
  .tableCard{
    h2{
      display: flex;
      align-items: center;
      justify-content: center;
      height: 60  px;
    }
    svg{
      height: 40px;
      margin: 0 40px;
    }
  }
</style>